package com.fairytask.ui.base
{
	import com.fairytask.data.Modulator;
	import com.fairytask.engine.components.window.Window;
	import com.fairytask.engine.components.window.WindowEvent;
	import com.fairytask.managers.BindManager;
	import com.fairytask.managers.ViewManage;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getQualifiedClassName;

	/**
	 * 基础UI框架 
	 * @author hanjy
	 * 
	 */	
	public class BaseUI extends Sprite
	{
		public var windowBG		:Window; //显示容器底层
		//数据
		protected var model		:Modulator	=	Modulator.instance;
		protected var bindManager	:BindManager	=	BindManager.instance;
		//属性
		protected var onshowFunc		:Function;
		protected var uiManage			:ViewManage;
		public var isInit				:Boolean=false; //是否初始化
		private var _popUiContainer	:Sprite; //气泡显示空间
		public var uiName		:String	=	"BaseUI";
		
		private var _actrueRect	:Rectangle;
		
		public function BaseUI()
		{
			super();
			uiManage	=	ViewManage.getInstance();
			uiName	=	getQualifiedClassName(this).split("::")[1];
			
			if(!isInit)
			{
				var fileName:String	=	getQualifiedClassName(this).split("::")[1];
				uiManage.initUI(this,fileName);
				isInit	=	true;
			}
			
			_popUiContainer	=	new Sprite();
			if(windowBG!=null)
			{
				windowBG.addChild(_popUiContainer);
				windowBG.addEventListener(WindowEvent.WINDOW_CLOSE, hide);
			}
		}
		
		/**
		 * 初始化显示对象 
		 * 
		 */
		public function initGUI():void
		{
		}
		
		/**
		 * 隐藏视图 
		 * @param e
		 */
		public function hide(e:*=null):void
		{
			
		}
		
		/**
		 * 显示界面 
		 */
		public function show(space:String="", onshow:Function=null):void
		{
			
		}
		
		/**
		 * 移除显示对象 
		 * @param value
		 * 
		 */
		protected function removeUI(value:DisplayObject):void
		{
			if(value.parent!=null)
			{
				//removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
				value.parent.removeChild(value);
			}
		}
		
		/**
		 * 界面隐藏后处理 
		 * 
		 */
		protected function AfterHide():void
		{
		}
		/**
		 * 添加气泡框UI	For 跟随拖动 
		 * @param baseUI
		 * 
		 */		
		public function addPopUI(baseUI:BaseUI):void
		{
			windowBG.addChildAt(_popUiContainer,windowBG.numChildren-1);
			_popUiContainer.addChild(baseUI);
		}
		
		/**
		 * 根据四个点来取得区域 
		 * @param sx 开始x
		 * @param sy 开始y
		 * @param ex 结束x
		 * @param ey 结束y
		 * @return 
		 * 
		 */
		public function getMaskRect(sx:int,sy:int,ex:int,ey:int):Rectangle
		{
			_actrueRect	=	new Rectangle(sx,sy,ex-sx,ey-sy);
			return new Rectangle(x+sx,y+sy,ex-sx,ey-sy);
		}
		
		/**
		 * 再次取得位置 
		 * @return 
		 * 
		 */
		public function regetMask():Rectangle
		{
			return new Rectangle(_actrueRect.x+x,_actrueRect.y+y,_actrueRect.width,_actrueRect.height);
		}
		
		
		/**
		 * 取得子对象区域 
		 * @param pList
		 * @return 
		 * 
		 */
		public function getItemRect(pList:Array):Rectangle
		{
			var tmpRect		:Rectangle;
			var tmpPoint	:Point	=	new Point(0,0);
			var tmpItem		:*;
			while(pList.length>0)
			{
				var tmpVar	:String	=	pList.shift();
				if(null!=tmpItem)
				{
					var itemType	:String	=	getQualifiedClassName(tmpItem).split("::")[1];
					if(itemType=="Sprite")
					{
						tmpItem	=	tmpItem.getChildByName(tmpVar);
					}
					else
					{
						tmpItem	=	tmpItem[tmpVar];
					}
				}
				else
				{
					tmpItem	=	this[tmpVar];
				}
				tmpPoint	=	tmpPoint.add(new Point(tmpItem.x,tmpItem.y));
			}
			if(null!=tmpItem)
			{
				_actrueRect	=	new Rectangle(tmpPoint.x,tmpPoint.y,tmpItem.width,tmpItem.height);
				tmpRect	=	new Rectangle(x+tmpPoint.x,y+tmpPoint.y,tmpItem.width,tmpItem.height);
			}
			return tmpRect;
		}
		
		/**
		 * ui重置 
		 * 
		 */
		public function reset():void
		{
			
		}
	}
}